package makovo.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Map;

import makovo.data.Resources;
import makovo.entities.SysUser;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class ApplicantRegister extends ActionSupport implements SessionAware {
	/**
	 * 
	 */
	private String mTextPattern = "^[a-zA-Z0-9áéíóúÁÉÍÓÚàèìòùÀÈÌÒÙñÑ \\-.]{1,}$";
	private String mNumberPatter = "^[0-9.,]{1,}$";
	private String mOnlyNumberPatter = "^[0-9]{1,}$";
	private String mDatePattern = "^[0-9]{4}/[0-9]{2}/[0-9]{2}$";

	private static final long serialVersionUID = 1L;
	private String mName;
	private String mLastName;
	private String mSecondLastName;
	private String mCurp;
	private String mDate;
	private String mTypeID;
	private String mFolioID;
	private String mSex;
	private String mPhoneNumber;
	private String mStatus;

	private String state;
	private String city;
	private String loc;
	private String extNumber;
	private String intNumber;
	private String street;
	private String cp;
	private String col;
	private int stateId;
	private int cityId;
	private int locId;

	private String lat;
	private String lonng;

	private Map<String, Object> mSession;

	public String getDate() {
		return this.mDate;
	}

	public void setDate(String date) {
		this.mDate = date;
	}

	public String getCol() {
		return this.col;
	}

	public void setCol(String col) {
		this.col = col;
	}
	
	public String getFolioID() {
		return this.mFolioID;
	}

	public void setFolioID(String folioID) {
		this.mFolioID = folioID;
	}

	public String getName() {
		return this.mName;
	}

	public void setName(String name) {
		try {
			mName = new String(name.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getLastName() {
		return this.mLastName;
	}

	public void setLastName(String lastName) {
		try {
			mLastName = new String(lastName.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getSecondLastName() {
		return this.mSecondLastName;
	}

	public void setSecondLastName(String secondLastName) {
		try {
			mSecondLastName = new String(secondLastName.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getCurp() {
		return this.mCurp;
	}

	public void setCurp(String curp) {
		this.mCurp = curp.toUpperCase();
	}

	public String getTypeID() {
		return this.mTypeID;
	}

	public void setTypeID(String typeID) {
		this.mTypeID = typeID;
	}

	public String getSex() {
		return this.mSex;
	}

	public void setSex(String sex) {
		this.mSex = sex;
	}

	public String getPhoneNumber() {
		return this.mPhoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.mPhoneNumber = phoneNumber;
	}

	public String getStatus() {
		return this.mStatus;
	}

	public void setStatus(String status) {
		this.mStatus = status;
	}

	public void setState(String state) {
		try {
			this.state = new String(state.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		try {
			this.city = new String(city.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getLoc() {
		return loc;
	}

	public void setLoc(String loc) {
		try {
			this.loc = new String(loc.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getExtNumber() {
		return extNumber;
	}

	public void setExtNumber(String extNumber) {
		this.extNumber = extNumber.toUpperCase();
	}

	public String getIntNumber() {
		return intNumber;
	}

	public void setIntNumber(String intNumber) {
		this.intNumber = intNumber.toUpperCase();
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		try {
			this.street = new String(street.getBytes("ISO-8859-1"), "UTF-8").toUpperCase();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getCp() {
		return cp;
	}

	public void setCp(String cp) {
		this.cp = cp;
	}

	public int getStateId() {
		return this.stateId;
	}

	public void setStateId(int stateId) {
		this.stateId = stateId;
	}

	public int getCityId() {
		return this.cityId;
	}

	public void setCityId(int cityId) {
		this.cityId = cityId;
	}

	public int getLocId() {
		return this.locId;
	}

	public void setLocId(int locId) {
		this.locId = locId;
	}

	public String getLat() {
		return this.lat;
	}

	public void setLat(String lat) {
		this.lat = lat;
	}

	public String getLonng() {
		return this.lonng;
	}

	public void setLonng(String lonng) {
		this.lonng = lonng;
	}

	public void validate() {
		try {
			SysUser u = Resources.getUserByMail((String) mSession.get("login.email"));
			if (u.userType == "4")
				if (stateId != u.idState || cityId != u.idCity) 
					addActionError("Acctión no soportada por capturista");

		} catch (Exception e) {
			e.printStackTrace();
		}

		if (!mCurp.matches(mTextPattern))
			addActionError("CURP");

		if (!mName.matches(mTextPattern))
			addActionError("Nombre del aplicante");

		if (!mLastName.matches(mTextPattern))
			addActionError("Apellido paterno del aplicante");

		if (!mSecondLastName.matches(mTextPattern))
			addActionError("Apellido materno del aplicante");

		if (!mDate.matches(mDatePattern))
			addActionError("fecha de Nacimiento");

		if (!mFolioID.matches(mTextPattern))
			addActionError("Id de Identificación");

		if (!street.matches(mTextPattern))
			addActionError("Calle de la empresa");

		if (!extNumber.matches(mTextPattern))
			addActionError("Número externo");

		if (!intNumber.matches(mTextPattern))
			addActionError("Número interno");

		if (stateId == -1)
			addActionError("Estado");

		if (cityId == -1)
			addActionError("Ciudad");

		if (!cp.matches(mOnlyNumberPatter))
			addActionError("Código postal");

	}

	public String execute() {
		int addressId = -1;
		try {
			addressId = Resources.saveAddress(state, stateId, city, cityId, loc, locId, cp, street, extNumber, intNumber,
					Double.valueOf(lonng), Double.valueOf(lat), col);

			if (Resources.saveApplicantInformation(mCurp, mName, mLastName, mSecondLastName, mDate, mTypeID, mFolioID, mStatus,
					mPhoneNumber, addressId, stateId, mSession.get("login.email").toString())) {

				this.mSession.put("User.idSender", mCurp);
				return "SUCCESS";

			}

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		return "NONE";
	}

	public String display() {
		return "NONE";
	}

	@Override
	public void setSession(Map<String, Object> sessionInfo) {
		this.mSession = sessionInfo;
	}
}
